<?php
	include_once('./db/config.php');
	
	function createPost($topicID ,$postTitle, $postBody, $username)
	{	
		$result = false;
		try
		{
			connectDb("tictocwatches");
			global $dbLink;
			$queryString = "call sp_createPost(".mysqli_real_escape_string($dbLink,$topicID).",'".mysqli_real_escape_string($dbLink,$postTitle)."','".mysqli_real_escape_string($dbLink,$postBody)."','".mysqli_real_escape_string($dbLink,$username)."')";
			$resultPointer = mysqli_query($dbLink,$queryString);
			
			if($resultPointer)
			{
				while($resultRow = mysqli_fetch_assoc($resultPointer))
				{
					$result = $resultRow["post_id"];
				}
				return $result;				
			}
			else
			{
				$queryError = mysqli_errno($dbLink);
				if($queryError!=0)
				{
					print_r($queryError);
				}
			}
		}
		catch(Exception $ex)
		{
			throw new Exception($ex->getMessage());
		}	
		return $result;
	}
	
	function removePost($topicID, $postID)
	{	
		$result = false;
		try
		{
			connectDb("tictocwatches");
			global $dbLink;
			$queryString = "call sp_removePost(".mysqli_real_escape_string($dbLink,$topicID).','.mysqli_real_escape_string($dbLink,$postID).")";
			$resultPointer = mysqli_query($dbLink,$queryString);
			
			if($resultPointer)
			{
				while($resultRow = mysqli_fetch_assoc($resultPointer))
				{
					$result = $resultRow["result"];
				}
				return $result;				
			}
			else
			{
				$queryError = mysqli_errno($dbLink);
				if($queryError!=0)
				{
					print_r($queryError);
				}
			}
		}
		catch(Exception $ex)
		{
			throw new Exception($ex->getMessage());
		}	
		return $result;
	}
	
	function updatePost($postID, $postTitle, $postBody)
	{	
		$result = false;
		try
		{
			connectDb("tictocwatches");
			global $dbLink;
			$queryString = "call sp_updatePost(".mysqli_real_escape_string($dbLink,$topicID).",'".mysqli_real_escape_string($dbLink,$postTitle)."','".mysqli_real_escape_string($dbLink,$postBody)."')";
			
			$resultPointer = mysqli_query($dbLink,$queryString);
			
			if($resultPointer)
			{
				while($resultRow = mysqli_fetch_assoc($resultPointer))
				{
					$result = $resultRow["result"];
				}
				return $result;				
			}
			else
			{
				$queryError = mysqli_errno($dbLink);
				if($queryError!=0)
				{
					print_r($queryError);
				}
			}
		}
		catch(Exception $ex)
		{
			throw new Exception($ex->getMessage());
		}	
		return $result;
	}
?>